#ifndef LANG_H_
#define LANG_H_
#include <ctype.h>
#include <string.h>
#include <math.h>
/**
 * 转置
 */
void reverse(char s[]) {
	int low = 0;
	int high = strlen(s) - 1;
	for (; low < high; low++, high--) {
		char temp = s[low];
		s[low] = s[high];
		s[high] = temp;
	}
}
/**
 * 将s转为整数
 */
int atoiL(char s[]) {
	int i, a, val = 0, sign;

	for (i = 0; isspace(s[i]); i++)
		;
	sign = (s[i] == '-') ? -1 : 1;

	if (s[i] == '+' || s[i] == '-')
		i++;

	while (isdigit(s[i])) {
		a = s[i] - '0';
		val = 10 * val + a;
		i++;
	}
	return val * sign;
}

/**
 * 将数字n转换为字符串数组
 */
void itoaL(int n, char s[]) {
	int sign = 1, i = 0;
	if (n < 0) {
		sign = -1;
	}
	do {
		s[i++] = fabs(n % 10) + '0';
		n = n / 10;
	} while (n != 0);

	if (sign == -1)
		s[i++] = '-';
	s[i] = '\0';
	reverse(s);
}
/**
 * 将数字n转换为字符串数组
 * b表示进制
 */
void itobL(int n, char s[], int b) {
	int i = 0, sign = n;
	do {
		int in = fabs(n % b);
		s[i++] = in > 9 ? in - 10 + 'a' : in + '0';
		n = n / b;
	} while (n != 0);
	if (sign < 0)
		s[i++] = '-';
	s[i] = '\0';
	reverse(s);
}
/**
 * 将数字n转换为字符串数组
 * c表示填充的位数
 */
void itocL(int n, char s[], int c) {
	int i = 0, sign = n;
	if (sign < 0) {
		n = -n;
	}
	do {
		s[i++] = n % 10 + '0';
		n = n / 10;
	} while (n > 0);
	if (sign < 0)
		s[i++] = '-';
	while (i < c)
		s[i++] = ' ';
	s[i] = '\0';
	reverse(s);
}
/**
 * 将字符串转换为浮点数
 */
double atofL(char s[]) {
	int i = 0, sign;
	double val = 0, power = 1;
	for (; isspace(s[i]); i++)
		;
	sign = s[i] == '-' ? -1 : 1;
	if (s[i] == '+' || s[i] == '-')
		i++;
	for (; isdigit(s[i]); i++) {
		val = 10 * val + (s[i] - '0');
	}
	if (s[i] == '.')
		i++;
	for (; isdigit(s[i]); i++) {
		val = 10 * val + (s[i] - '0');
		power *= 10.0;
	}
	val = val / power;
	return val * sign;
}
/**
 * 将字符串转换为浮点数
 */
double atofeL(char s[]) {
	int i = 0, sign;
	double val = 0, power = 1, flag, val2 = 0;
	for (; isspace(s[i]); i++)
		;
	sign = s[i] == '-' ? -1 : 1;
	if (s[i] == '+' || s[i] == '-')
		i++;
	for (; isdigit(s[i]); i++) {
		val = 10 * val + (s[i] - '0');
	}
	if (s[i] == '.')
		i++;
	for (; isdigit(s[i]); i++) {
		val = 10 * val + (s[i] - '0');
		power *= 10.0;
	}
	val = val / power;
	if (s[i] == 'e' || s[i] == 'E') {
		i++;
		flag = s[i] == '-' ? -1 : 1;
		if (s[i] == '+' || s[i] == '-')
			i++;

		for (; isdigit(s[i]); i++) {
			val2 = 10 * val2 + (s[i] - '0');
		}
		if (flag == 1)
			while (val2-- > 0)
				val *= 10.0;
		else
			while (val2-- > 0)
				val /= 10.0;
	}

	return val * sign;
}
/**
 * 删除字符串尾部的空格、制表符、换行符
 */
int trim(char s[]) {
	int i = strlen(s) - 1;
//	while (s[i] != ' ' || s[i] != '\t' || s[i] != '\n')
//		i++;
//	s[i]='\0';

	for (; i >= 0; i--) {
		if (s[i] != ' ' && s[i] != '\t' && s[i] != '\n'
			)
			break;
	}
	s[i + 1] = '\0';
	return i;
}
int strcmpL(char *s, char *t) {
	while (*s == *t){
		if(*s=='\0')
			return 0;
		s++;
		t++;
	}
	return *s - *t;
}

#endif
